package com.gklyj.controller.qiuzhi;

import com.gklyj.pojo.User;
import com.gklyj.pojo.Zhanghao;
import com.gklyj.service.userService;
import com.gklyj.service.zhanghaoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@Transactional
public class Qz_centerController {

    @Autowired
    userService userService;

    @Autowired
    zhanghaoService zhanghaoService;
    /*
    修改个人中心资料
     */
    @PostMapping("/updateUserData")
    @ResponseBody
    public boolean updateUserData(@RequestBody User user) throws RuntimeException{
        return userService.updateById(user);
    }

    /*
    跳转修改密码窗口
     */
    @GetMapping("/toChangePassword")
    public String toChangePassword(HttpServletRequest request,Model model){
        Object username = request.getSession().getAttribute("username");
        Zhanghao zhanghao = zhanghaoService.FindZhangHaoByUsername((String) username);
        model.addAttribute("zhanghao",zhanghao);
        return "qiuzhi/qz-change-password";
    }

    /*
    更新修改后的密码
     */
    @PostMapping("/updatePassword")
    @ResponseBody
    public boolean updatePassword(@RequestBody Zhanghao zhanghao) throws RuntimeException{
        return zhanghaoService.updateById(zhanghao);
    }
}
